//
//  CSPageEngine.m
//  Pods
//
//  Created by 网新中研 on 2017/4/25.
//
//

#import "CSPageEngine.h"

@interface CSPageEngine()

@property (nonatomic, strong) NSDictionary *descriptor;

@property (nonatomic, strong) UIView *restSpaceView;

@end

@implementation CSPageEngine

static NSMutableDictionary *_resourceBase;

/**
 * 载入图片资源
 */
+(void)loadImage:(UIImage *)image forKey:(NSString *)key{
    
    if (_resourceBase == nil) {
        _resourceBase = [NSMutableDictionary new];
    }
    
    if (image == nil
        || [key isEqualToString:@""]
        || key == nil) {
        return;
    }
    
    [_resourceBase setObject:image forKey:key];
    
}

/**
 * 获取图片
 */
+(UIImage *)getImageWithKey:(NSString *)key{
    if (_resourceBase !=nil
        && key != nil
        && ![key isEqualToString:@""]) {
        return [_resourceBase objectForKey:key];
    }
    return nil;
}

/**
 * 数据引擎工厂
 */
+(id)pageEngine:(NSDictionary *)descriptor{
    
    CSPageEngine *pageEngine = [CSPageEngine new];
    pageEngine.descriptor = descriptor;
    [pageEngine parsePage];
    return pageEngine;
    
}


/**
 * 载入页面
 */
-(void)loadPage:(UIView *)superView{
    _pageView = [self inflatePage:NO];
    [superView addSubview:_pageView];
}

/**
 * 刷新页面
 */
-(void)refreshPage{
    
    if (_pageModel.pageView != nil) {
        
        [self inflatePage:NO];
        
    }
    
}

/**
 * 刷新指定页面的值
 * @params pageModel: 要刷新的页面对象
 */
-(void)refreshPageWithPageModel:(CSPageModel *)pageModel{
    
    if (pageModel == nil
        || pageModel.pageView == nil) {
        return;
    }
    
    [self inflatePageWithPageModel:pageModel];
    
}

/**
 * 实例化一个页面
 */
-(CSPageModel *)parsePage{
    
    if (_descriptor == nil) {
        return nil;
    }
    
    NSDictionary *descriptor = _descriptor;
    
    CSPageModel *pageModel = [CSPageModel new];
    
    pageModel.id = [descriptor objectForKey:@"page_id"];
    pageModel.pageTitle = [descriptor objectForKey:@"page_title"];
    
    //获取页面布局元素
    NSArray *pageElements = [descriptor objectForKey:@"page_elements"];
    for (int i = 0; i < pageElements.count; i++) {
        
        NSDictionary *elementDic = [pageElements objectAtIndex:i];
        CSPageElementModel *pageElementModel = [CSPageElementModel new];
        pageElementModel.type = [[elementDic objectForKey:@"type"] integerValue];
        pageElementModel.title = [elementDic objectForKey:@"title"];
        pageElementModel.defaultValue = [elementDic objectForKey:@"defaultValue"];
        pageElementModel.subType = [[elementDic objectForKey:@"subType"] integerValue];
        pageElementModel.json = [elementDic objectForKey:@"json"];
        pageElementModel.isPassword = [[elementDic objectForKey:@"isPassword"] boolValue];
        pageElementModel.placeHolder = [elementDic objectForKey:@"placeHolder"];
        pageElementModel.isSectionBreak = [[elementDic objectForKey:@"isSectionBreak"] boolValue];
        pageElementModel.textAlignment = [[elementDic objectForKey:@"textAlignment"] intValue];
        pageElementModel.pageEngine = self;
        [pageModel.elements addObject:pageElementModel];
        
    }
    
    //获取页面请求元素
    NSArray *pageRequests = [descriptor objectForKey:@"page_requests"];
    for (int i = 0; i < pageRequests.count; i++) {
        
        //获取请求地址
        NSDictionary *requsetDic = [pageRequests objectAtIndex:i];
        CSPageRequestModel *pageRequestModel = [CSPageRequestModel new];
        pageRequestModel.requestUrl = [requsetDic objectForKey:@"url"];
        
        
        //获取请求参数
        NSArray *parameters = [requsetDic objectForKey:@"parameters"];
        if (parameters == nil) {
            continue;
        }
        for (int j = 0; j < parameters.count; j++) {
            
            NSDictionary *parameterDic = [parameters objectAtIndex:j];
            CSPageParameterModel *parameterModel = [CSPageParameterModel new];
            parameterModel.json = [parameterDic objectForKey:@"json"];
            parameterModel.value = [parameterDic objectForKey:@"value"];
            //请求对象添加参数
            [pageRequestModel.parameters addObject:parameterModel];
            
        }
        
        [pageModel.requests addObject:pageRequestModel];
        
    }
    
    _pageModel = pageModel;
    
    return pageModel;
    
}

/**
 * 移除指定页面元素
 */
-(void)removeElement:(CSPageElementModel *)pageElementModel{
    if (self.pageModel
        &&self.pageModel.elements) {
        
        [self.pageModel.elements removeObject:pageElementModel];                                              

    }
}

/**
 * 将页面对象转化为视图
 */
-(UIView *)inflatePageWithPageModel:(CSPageModel *)pageModel{
    
    if (pageModel == nil) {
        return nil;
    }
    
    //    if (_pageView != nil) {
    //        [_pageView removeFromSuperview];
    //    }
    
    if (pageModel.elements != nil) {
        
        //实例化页面对象
        for (CSPageElementModel *pageElementModel in pageModel.elements) {
            
            if (![pageElementModel isKindOfClass:[CSPageElementModel class]]) {
                continue;
            }
            
            if (pageElementModel.viewInstance) {
                
                pageElementModel.viewInstance.isDevelopmentMode = self.isDevelopmentMode;
                if (pageElementModel.value == nil) {
                    if (pageElementModel.defaultValue != nil) {
                        pageElementModel.viewInstance.value = pageElementModel.defaultValue;
                    }else{
                        pageElementModel.viewInstance.value = @"";
                    }
                }else{
                    pageElementModel.viewInstance.value = pageElementModel.value;
                }
                
                continue;
                
            }
            
            switch (pageElementModel.type) {
                case CSViewType_PageSectionTitleView:
                {
                    switch (pageElementModel.subType) {
                        case SectionTitleType_Basic:
                        case SectionTitleType_Edit:
                        {
                            CS_Page_SectionTitleView *pageSectionTitleView = [CS_Page_SectionTitleView
                                                                              page_SectionTitleView:pageElementModel.title
                                                                              color:COLOR_THEME sectionTitleType:(SectionTitleType)pageElementModel.subType];
                            pageElementModel.viewInstance = pageSectionTitleView;
                        }
                            break;
                        default:
                        {
                            CS_Page_SectionTitleView *pageSectionTitleView = [CS_Page_SectionTitleView
                                                                              page_SectionTitleView:pageElementModel.title
                                                                              color:COLOR_THEME image:nil];
                            pageElementModel.viewInstance = pageSectionTitleView;
                        }
                            break;
                    }
                }
                    break;
                case CSViewType_PageTextView:{
                    
                    CS_Page_TextView *pageTextView = [CS_Page_TextView page_textView:pageElementModel.title
                                                                           dataValue:(pageElementModel.value == nil ? pageElementModel.defaultValue : pageElementModel.value)];
                    pageTextView.jsonField = pageElementModel.json;
                    pageElementModel.viewInstance = pageTextView;
                    
                }
                    break;
                case CSViewType_PageTextFieldView:{
                    
                    CS_Page_TextFieldView *pageTextView = [CS_Page_TextFieldView textFiledWithPlaceHolder:pageElementModel.placeHolder
                                                                                             keyboardType:UIKeyboardTypeDefault
                                                                                               isPassword:pageElementModel.isPassword];
                    pageTextView.jsonField = pageElementModel.json;
                    pageTextView.textFieldType = (CSTextFieldType)pageElementModel.subType;
                    pageElementModel.viewInstance = pageTextView;
                    
                    
                }
                    break;
                case CSViewType_PageImageView:{
                    
                    CS_Page_ImageView *pageTextView = [CS_Page_ImageView page_ImageView:pageElementModel.title
                                                                                 images:pageElementModel.defaultValue
                                                                                   mark:nil];
                    pageTextView.jsonField = pageElementModel.json;
                    pageElementModel.viewInstance = pageTextView;
                    
                }
                    break;
                case CSViewType_Page_Button:{
                    
                    CS_Page_Button *button = [CS_Page_Button new];
                    button.buttonName = pageElementModel.title;
                    button.clickHandler = pageElementModel.clickHandler;
                    pageElementModel.viewInstance = button;
                    
                }
                    break;
                case CSViewType_PageDescriptionView:{
                    
                    CS_Page_DescriptionView *descriptionView = [CS_Page_DescriptionView descriptionViewWithTitle:pageElementModel.title
                                                                                                         content:pageElementModel.defaultValue];
                    descriptionView.textAlignment = pageElementModel.textAlignment;
                    pageElementModel.viewInstance = descriptionView;
                    
                }
                    break;
                case CSViewType_PageSpaceView:{
                    
                    CS_Page_SpaceView *spaceView = [CS_Page_SpaceView new];
                    spaceView.spaceSize = [pageElementModel.value intValue];
                    pageElementModel.viewInstance = spaceView;
                    
                }
                    break;
                default:
                    break;
            }
            
#warning 双向引用处理 这里viewInstance(CSBaseView)的pageElementModel用的是assign进行修饰
            if (pageElementModel.viewInstance) {
                pageElementModel.viewInstance.pageElementModel = pageElementModel;
            }
            
            pageElementModel.viewInstance.isDevelopmentMode = self.isDevelopmentMode;
            
        }
        
        pageModel.pageView = [self getPageView:pageModel superScrollView:pageModel.pageView];
        
        return pageModel.pageView;
        
    }
    return nil;
}

/**
 * 将页面对象转化为视图
 */
-(UIView *)inflatePage:(BOOL)isNavigationBarVisible{
    
    if (_pageModel == nil) {
        return nil;
    }
    
//    if (_pageView != nil) {
//        [_pageView removeFromSuperview];
//    }
    
    if (_pageModel.elements != nil) {
        
        //实例化页面对象
        for (CSPageElementModel *pageElementModel in _pageModel.elements) {
            
            if (![pageElementModel isKindOfClass:[CSPageElementModel class]]) {
                continue;
            }
            
            if (pageElementModel.viewInstance) {
                
                pageElementModel.viewInstance.isDevelopmentMode = self.isDevelopmentMode;
                if (pageElementModel.value == nil) {
                    if (pageElementModel.defaultValue != nil) {
                        pageElementModel.viewInstance.value = pageElementModel.defaultValue;
                    }else{
                        pageElementModel.viewInstance.value = @"";
                    }
                }else{
                    pageElementModel.viewInstance.value = pageElementModel.value;
                }
                
                continue;
                
            }
            
            switch (pageElementModel.type) {
                case CSViewType_PageSectionTitleView:
                {
                    switch (pageElementModel.subType) {
                        case SectionTitleType_Basic:
                        case SectionTitleType_Edit:
                        {
                            CS_Page_SectionTitleView *pageSectionTitleView = [CS_Page_SectionTitleView
                                                                              page_SectionTitleView:pageElementModel.title
                                                                              color:COLOR_THEME sectionTitleType:(SectionTitleType)pageElementModel.subType];
                            pageElementModel.viewInstance = pageSectionTitleView;
                        }
                            break;
                        default:
                        {
                            CS_Page_SectionTitleView *pageSectionTitleView = [CS_Page_SectionTitleView
                                                                              page_SectionTitleView:pageElementModel.title
                                                                              color:COLOR_THEME image:nil];
                            pageElementModel.viewInstance = pageSectionTitleView;
                        }
                            break;
                    }
                }
                    break;
                case CSViewType_PageTextView:{
                    
                    CS_Page_TextView *pageTextView = [CS_Page_TextView page_textView:pageElementModel.title
                                                                           dataValue:(pageElementModel.value == nil ? pageElementModel.defaultValue : pageElementModel.value)];
                    pageTextView.jsonField = pageElementModel.json;
                    pageElementModel.viewInstance = pageTextView;
                    
                }
                    break;
                case CSViewType_PageTextFieldView:{
                    
                    CS_Page_TextFieldView *pageTextView = [CS_Page_TextFieldView textFiledWithPlaceHolder:pageElementModel.placeHolder
                                                                                             keyboardType:UIKeyboardTypeDefault
                                                                                               isPassword:pageElementModel.isPassword];
                    pageTextView.jsonField = pageElementModel.json;
                    pageTextView.textFieldType = (CSTextFieldType)pageElementModel.subType;
                    if (pageTextView.textFieldType == CSTextFieldType_Phone) {
                        pageTextView.keyboardType = UIKeyboardTypePhonePad;
                    }else if(pageTextView.textFieldType == CSTextFieldType_Email){
                        pageTextView.keyboardType = UIKeyboardTypeEmailAddress;
                    }else if(pageTextView.textFieldType == CSTextFieldType_PostCode){
                        pageTextView.keyboardType = UIKeyboardTypeNumberPad;
                    }
                    pageElementModel.viewInstance = pageTextView;
                    
                }
                    break;
                case CSViewType_PageImageView:{
                    
                    CS_Page_ImageView *pageTextView = [CS_Page_ImageView page_ImageView:pageElementModel.title
                                                                                 images:pageElementModel.defaultValue
                                                                                   mark:nil];
                    pageTextView.jsonField = pageElementModel.json;
                    pageElementModel.viewInstance = pageTextView;
                    
                }
                    break;
                case CSViewType_Page_Button:{
                    
                    CS_Page_Button *button = [CS_Page_Button new];
                    button.buttonName = pageElementModel.title;
                    button.clickHandler = pageElementModel.clickHandler;
                    pageElementModel.viewInstance = button;
                    
                }
                    break;
                case CSViewType_PageDescriptionView:{
                        
                    CS_Page_DescriptionView *descriptionView = [CS_Page_DescriptionView descriptionViewWithTitle:pageElementModel.title
                                                                                                             content:pageElementModel.defaultValue];
                    descriptionView.textAlignment = pageElementModel.textAlignment;
                    pageElementModel.viewInstance = descriptionView;
                        
                    }
                    break;
                case CSViewType_PageSpaceView:{
                    
                    CS_Page_SpaceView *spaceView = [CS_Page_SpaceView new];
                    spaceView.spaceSize = [pageElementModel.value intValue];
                    pageElementModel.viewInstance = spaceView;
                    
                }
                    break;
                default:
                    break;
            }
            
#warning 双向引用处理 这里viewInstance(CSBaseView)的pageElementModel用的是assign进行修饰
            if (pageElementModel.viewInstance) {
                pageElementModel.viewInstance.pageElementModel = pageElementModel;
            }
            
            pageElementModel.viewInstance.isDevelopmentMode = self.isDevelopmentMode;
            
        }
        
        
        //将处理后的page视图和pagemodel绑定，切断和pageengine的联系
        _pageModel.pageView = [self getPageView:_pageModel superScrollView:_pageModel.pageView];
        
        return _pageModel.pageView;
    }
    return nil;
}

-(UIScrollView *)getPageView:(CSPageModel *)pageModel superScrollView:(UIScrollView *)scrollView{
    
    if (scrollView == nil) {
        scrollView = [UIScrollView new];
    }
    
    REMOVE_SUBVIEWS(scrollView);
    
    CGFloat primaryY = 0;
    
    scrollView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    
    CS_Page_SectionTitleView *sectionView = nil;
    
    //布局页面
    for (NSObject *element in pageModel.elements) {
        
        
        if ([element isKindOfClass:[CSPageElementModel class]]) {
            
            CSPageElementModel *pageElementModel = (CSPageElementModel *)element;
            
            if (pageElementModel.viewInstance) {
                
                if(pageElementModel.type == CSViewType_Page_Button
                   ||pageElementModel.isSectionBreak){
                    
                    if (sectionView != nil) {
                        primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
                        sectionView = nil;
                    }
                    
                    primaryY = [pageElementModel.viewInstance layoutView:scrollView primaryY:primaryY];
                    
                }else if (pageElementModel.type == CSViewType_PageSectionTitleView) {
                    
                    if (sectionView != nil) {
                        primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
                    }
                    
                    [((CS_Page_SectionTitleView *)pageElementModel.viewInstance).sectionItems removeAllObjects];
                    sectionView = (CS_Page_SectionTitleView *)pageElementModel.viewInstance;
                    
                }else{
                    if (sectionView != nil) {
                        
                        [sectionView.sectionItems addObject:pageElementModel.viewInstance];
                        
                    }else{
                        
                        primaryY = [pageElementModel.viewInstance layoutView:scrollView primaryY:primaryY];
                        
                    }
                }
                
            }
        }else if([element isKindOfClass:[CSBaseView class]]){
            CSBaseView *baseView = (CSBaseView *)element;
            primaryY = [baseView layoutView:scrollView primaryY:primaryY];
        }
        
        
    }
    
    if (sectionView != nil) {
        primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
    }
    
    scrollView.contentSize = CGSizeMake(0, primaryY);
    
    if (primaryY < scrollView.frame.size.height) {
        CGFloat restHeight = scrollView.frame.size.height - primaryY;
        self.pageModel.restSpaceView.frame = CGRectMake(0, primaryY, kWindowW, restHeight);
        [scrollView addSubview:self.pageModel.restSpaceView];
    }else{
        self.pageModel.restSpaceView = nil;
    }
    
    return scrollView;
}




-(UIScrollView *)getPageView:(CSPageModel *)pageModel isNavigationBarVisible:(BOOL)isNavigationBarVisible{
    
    CGFloat primaryY = 0;
    
    UIScrollView *scrollView = [[UIScrollView alloc]init];
    
    if (isNavigationBarVisible) {
        scrollView.frame = CGRectMake(0, HEIGHT_NAVIGATIONBAR, kWindowW, HEIGHT_CONTENT);
    }else{
        scrollView.frame = CGRectMake(0, 0, kWindowW, kWindowH);
    }
    
    CS_Page_SectionTitleView *sectionView = nil;
    
    //布局页面
    for (CSPageElementModel *pageElementModel in pageModel.elements) {
        
        if (pageElementModel.viewInstance) {
            
            pageElementModel.viewInstance.layer.opacity = 1.0;
            
            if(pageElementModel.type == CSViewType_Page_Button){
                
                if (sectionView != nil) {
                    primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
                    sectionView = nil;
                }
                
                primaryY = [pageElementModel.viewInstance layoutView:scrollView primaryY:primaryY];
                
            }else if (pageElementModel.type == CSViewType_PageSectionTitleView) {
                
                if (sectionView != nil) {
                    primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
                }
                
                [((CS_Page_SectionTitleView *)pageElementModel.viewInstance).sectionItems removeAllObjects];
                sectionView = (CS_Page_SectionTitleView *)pageElementModel.viewInstance;
                
            }else{
                if (sectionView != nil) {
                    
                    [sectionView.sectionItems addObject:pageElementModel.viewInstance];
                    
                }else{
                    
                    primaryY = [pageElementModel.viewInstance layoutView:scrollView primaryY:primaryY];
                    
                }
            }
            
        }
        
    }
    
    if (sectionView != nil) {
        primaryY = [sectionView layoutView:scrollView primaryY:primaryY];
    }
    
    scrollView.contentSize = CGSizeMake(0, primaryY);
    
    return scrollView;
}

-(void)refreshPageValue:(id)responseObj{
    
    if(self.pageModel.elements == nil) return;
    
    //从返回的字典中映射页面对象值
    for (CSPageElementModel *pageElementModel in self.pageModel.elements) {
        
        if (pageElementModel.json != nil && ![pageElementModel.json isEqualToString:@""]) {
            
            NSString *valueString = (NSString *)[responseObj objectForKey:pageElementModel.json];
            
            if (valueString != nil) {
                pageElementModel.value = valueString;
            }
        }
        
    }
    
    //刷新视图上显示的值
    return [self refreshPageWithPageModel:self.pageModel];
    
}

/**
 * 滚动到底部视图
 */
-(void)scrollToBottom{
    
}

/**
 * 隐藏键盘
 */
-(void)hideKeyboard{
    if (self.pageModel.pageView != nil) {
        for (CSPageElementModel *pageElement in self.pageModel.elements) {
            if ([pageElement isKindOfClass:[CSPageElementModel class]]) {
                [pageElement.viewInstance hideKeyboard];
            }
            
        }
    }
}

/**
 * 显示元素
 */
-(void)showElementInTheList:(CSPageElementModel *)pageElementModel targetView:(UIView *)targetView{
    
    //上下滑动视图
    [self.pageModel.pageView setScrollEnabled:NO];
//    pageElementModel.viewInstance.frame = CGRectMake(<#CGFloat x#>, <#CGFloat y#>, <#CGFloat width#>, <#CGFloat height#>)
//    
//    
//    
//    
    
    
    //显示目标视图
    
}

/**
 * 打开新的页面:采用上下移出界面的方式
 * @parms:pageElementModel 页面对象 点击后的页面对象
 * @parms:targetView 要显示的视图对象
 */
-(void)dismissShowElement:(CSPageElementModel *)pageElementModel targetView:(UIView *)targetView{
    
    if (pageElementModel.viewInstance == nil) {
        
        @throw [NSException exceptionWithName:@"错误：" reason:@"PageEngine试图显示pageElemodel中viewInstance对象,但是viewInstance对象为空" userInfo:nil];
        
        return;
    }
    
    NSInteger index = [self.pageModel.elements indexOfObject:pageElementModel];
    
    CSBaseView *clickView = pageElementModel.viewInstance;
    CGRect frame = clickView.frame;
    CGFloat topDeviation = frame.origin.y - self.pageModel.pageView.contentOffset.y;
    
    CGFloat bottomDeviation = self.pageModel.pageView.frame.size.height - topDeviation - clickView.frame.size.height;
    
    self.pageModel.pageView.scrollEnabled = NO;
    
    __block CSPageElementModel *weakPageElementModel = pageElementModel;
    
    [self.pageModel.pageView.superview insertSubview:targetView belowSubview:self.pageModel.pageView];
    
    self.pageModel.pageView.backgroundColor = [UIColor clearColor];
    
    WS(weakSelf);
    
//    weakPageElementModel.viewInstance.layer.shadowOffset = CGSizeMake(0, 0.1);
//    weakPageElementModel.viewInstance.layer.shadowColor = [UIColor blackColor].CGColor;
//    weakPageElementModel.viewInstance.layer.shadowOpacity = 0.1;
    
    [UIView animateWithDuration:0.1 animations:^{
        weakPageElementModel.viewInstance.layer.opacity = 0;
        
        
        //        weakPageElementModel.viewInstance.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y - 10, originalFrame.size.width, originalFrame.size.height + 20);
    } completion:^(BOOL finished) {
        [weakPageElementModel.viewInstance removeFromSuperview];
        
        
    }];
    
    CGFloat percent = 1;
    
    //设置阴影效果
    CSPageElementModel *elementModel = [weakSelf.pageModel.elements objectAtIndex:index - 1];
    elementModel.viewInstance.layer.shadowColor = [UIColor blackColor].CGColor;
    elementModel.viewInstance.layer.shadowOffset = CGSizeMake(0, 0.1);
    elementModel.viewInstance.layer.shadowOpacity = 0.2;
    
    //设置阴影效果
    CSPageElementModel *elementModel1 = [weakSelf.pageModel.elements objectAtIndex:index + 1];
    elementModel1.viewInstance.layer.shadowColor = [UIColor blackColor].CGColor;
    elementModel1.viewInstance.layer.shadowOffset = CGSizeMake(0, 0.1);
    elementModel1.viewInstance.layer.shadowOpacity = 0.2;
    
    
    [UIView animateWithDuration:0.85 delay:0 usingSpringWithDamping:1 initialSpringVelocity:1 options:UIViewAnimationOptionCurveLinear animations:^{
        for (int i = 0; i < index; i++) {
            CSPageElementModel *elementModel = [weakSelf.pageModel.elements objectAtIndex:i];
            CGRect originFrame = elementModel.viewInstance.frame;
            elementModel.viewInstance.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y - topDeviation * percent, originFrame.size.width, originFrame.size.height);
            
//            elementModel.viewInstance.layer.opacity = 0.5;
            
        }
        
        
        for (long i = (index + 1); i < weakSelf.pageModel.elements.count; i++) {
            CSPageElementModel *elementModel = [weakSelf.pageModel.elements objectAtIndex:i];
            CGRect originFrame = elementModel.viewInstance.frame;
            elementModel.viewInstance.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y + bottomDeviation * percent, originFrame.size.width, originFrame.size.height);
            
//            elementModel.viewInstance.layer.opacity = 0.5;
        }
        
        if (self.restSpaceView.superview !=nil) {
            CGRect originFrame = self.restSpaceView.frame;
            self.restSpaceView.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y + bottomDeviation * percent, originFrame.size.width, originFrame.size.height);
            
//            self.restSpaceView.layer.opacity = 0.9;
        }
        
//        pageElementModel.viewInstance.layer.opacity = 0.0;
        
        
        
    }completion:^(BOOL finished) {
        
        weakSelf.pageModel.pageView.scrollEnabled = YES;
        
        for (int i = 0; i < self.pageModel.elements.count; i++) {
            CSPageElementModel *model = [weakSelf.pageModel.elements objectAtIndex:i];
            [model.viewInstance removeFromSuperview];
            
        }
        [weakSelf.pageModel.pageView removeFromSuperview];
        
    }];

    
}

/**
 * 显示指定的页面对象:采用上下移出界面的方式显示指定的视图对象
 * @parms:pageElementModel 页面对象 点击后的页面对象
 * @parms:pageModel 要显示的页面对象
 *
 */
-(void)dismissShowElement:(CSPageElementModel *)pageElementModel targetPage:(CSPageModel *)pageModel{
    
    UIView *targetView = [self inflatePageWithPageModel:pageModel];
    
    [self dismissShowElement:pageElementModel targetView:targetView];
    
}

-(void)showPage:(CSPageElementModel *)pageElementModel{
    
    if (pageElementModel.viewInstance == nil) {
        @throw [NSException exceptionWithName:@"错误：" reason:@"PageEngine试图显示pageElemodel中viewInstance对象,但是viewInstance对象为空" userInfo:nil];
        return;
    }
    
    NSInteger index = [self.pageModel.elements indexOfObject:pageElementModel];
    
    CSBaseView *clickView = pageElementModel.viewInstance;
    CGRect frame = clickView.frame;
    CGFloat topDeviation = frame.origin.y - self.pageModel.pageView.contentOffset.y;
    CGFloat bottomDeviation = self.pageModel.pageView.frame.size.height - topDeviation - clickView.frame.size.height;
    
    self.pageModel.pageView.scrollEnabled = NO;
    
    __block CSPageElementModel *weakPageElementModel = pageElementModel;
        
    [UIView animateWithDuration:0.6 animations:^{
        weakPageElementModel.viewInstance.layer.opacity = 0;
        
        
//        weakPageElementModel.viewInstance.frame = CGRectMake(originalFrame.origin.x, originalFrame.origin.y - 10, originalFrame.size.width, originalFrame.size.height + 20);
    } completion:^(BOOL finished) {
        [weakPageElementModel.viewInstance removeFromSuperview];
    }];
    
    WS(weakSelf);
    [UIView animateWithDuration:1.2 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
        for (int i = 0; i < index; i++) {
            CSPageElementModel *elementModel = [weakSelf.pageModel.elements objectAtIndex:i];
            CGRect originFrame = elementModel.viewInstance.frame;
            elementModel.viewInstance.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y - topDeviation, originFrame.size.width, originFrame.size.height);
            
            elementModel.viewInstance.layer.opacity = 0.9;
            
        }
        
        
        for (long i = (index + 1); i < weakSelf.pageModel.elements.count; i++) {
            CSPageElementModel *elementModel = [weakSelf.pageModel.elements objectAtIndex:i];
            CGRect originFrame = elementModel.viewInstance.frame;
            elementModel.viewInstance.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y + bottomDeviation, originFrame.size.width, originFrame.size.height);
            
            elementModel.viewInstance.layer.opacity = 0.5;
        }
        
        if (self.restSpaceView.superview !=nil) {
            CGRect originFrame = self.restSpaceView.frame;
            self.restSpaceView.frame = CGRectMake(originFrame.origin.x, originFrame.origin.y + bottomDeviation, originFrame.size.width, originFrame.size.height);
            
            self.restSpaceView.layer.opacity = 0.5;
        }
        
        pageElementModel.viewInstance.layer.opacity = 0.0;
        
        

    }completion:^(BOOL finished) {
        weakSelf.pageModel.pageView.scrollEnabled = YES;
        
        for (int i = 0; i < self.pageModel.elements.count; i++) {
            CSPageElementModel *model = [weakSelf.pageModel.elements objectAtIndex:i];
            [model.viewInstance removeFromSuperview];
        }
    }];
}


@end
